﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Win32;

    /// <summary>Provides the means to connect to a test server that requires authentication.</summary>
    internal static class TestServerHelper
    {
        private const string RegistryKey = "HKEY_CURRENT_USER\\Software\\Mews\\TestServer";
        private const string ServerNameValueName = "ServerName";
        private const string AccountNameValueName = "AccountName";
        private const string PasswordValueName = "Password";

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal static Client GetTestServerClient()
        {
            while (true)
            {
                using (var form = new TestServerForm(ServerName, AccountName, Password))
                {
                    try
                    {
                        using (var clientGuard = ScopeGuard.Create(new Client(form.ServerName)))
                        {
                            new AuthInfoCommand(form.AccountName, form.Password).Execute(clientGuard.Resource);
                            clientGuard.Dismiss();
                            return clientGuard.Resource;
                        }
                    }
                    catch (ArgumentNullException)
                    {
                    }
                    catch (AuthenticationFailedException)
                    {
                    }
                    catch (IOException)
                    {
                    }

                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        SetRegistryValue(ServerNameValueName, form.ServerName);
                        SetRegistryValue(AccountNameValueName, form.AccountName);
                        SetRegistryValue(PasswordValueName, form.Password);
                    }
                    else
                    {
                        Assert.Inconclusive("Test server is not available.");
                    }
                }
            }
        }

        internal static string ServerName
        {
            get { return GetRegistryValue(ServerNameValueName); }
        }

        internal static string AccountName
        {
            get { return GetRegistryValue(AccountNameValueName); }
        }

        internal static string Password
        {
            get { return GetRegistryValue(PasswordValueName); }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private static string GetRegistryValue(string valueName)
        {
            return (string)Registry.GetValue(RegistryKey, valueName, null);
        }

        private static void SetRegistryValue(string valueName, string value)
        {
            Registry.SetValue(RegistryKey, valueName, value);
        }
    }
}
